﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.IViews.Admin;
using Portfolio.IViews.Common;
using Portfolio.BL.ArticleCategories;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Presenters.Interfaces;
using Portfolio.BL.Common;
using Portfolio.BL.Dictionary;

namespace Portfolio.Presenters.Admin
{
    /// <summary>
    /// Klasa presenter list ArticleCategories
    /// </summary>
    public class AdminArticleCategoriesPresenter : IDefaultPresenter
    {
        private IAdminArticleCategoriesView _view;        
        private ArticleCategoriesBL _articleCategoriesBL;

        /// <summary>
        /// Konstruktor presenter.
        /// </summary>
        /// <param name="view">Interfejs widoku strony</param>
        public AdminArticleCategoriesPresenter(IAdminArticleCategoriesView view)
        {
            _view = view;
            _articleCategoriesBL = new ArticleCategoriesBL();
            this.Initialize();
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Inicjalizuje wszystkie zdarzenia na stronie
        /// </summary>
        public void Initialize()
        {
            this._view.LoadArticleCategoriesSortBySearch += new EventHandler<EventArgs>(_viewLoadArticleCategoriesSortBySearch);
            this._view.DeleteArticleCategory += new EventHandler<EventArgs>(_viewDeleteArticleCategory);
            this._view.LoadFieldsSearch += new EventHandler<EventArgs>(_viewLoadFieldsSearch);
            this._view.SetMaxSeqArticleCategory += new EventHandler<EventArgs>(_viewSetMaxSeqArticleCategory);
            this._view.SetUpSeq += new EventHandler<EventArgs>(_viewSetUpSeq);
            this._view.SetDownSeq += new EventHandler<EventArgs>(_viewSetDownSeq);
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie wykonuje sortowanie kategorii przez podane parametry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewLoadArticleCategoriesSortBySearch(object sender, EventArgs e)
        {
            _view.ArticleCategoriesDataBind = _articleCategoriesBL.ReturnListGridArticleCategoriesBySearch(_view.SearchTitle, _view.SearchArticleCategories, _view.SearchStatus, _view.SearchActive, _view.SortExpression, _view.SortDirection);
            _view.PublishException = _articleCategoriesBL.InformationException;            
        }

        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie zlicza wszystkie kategorie w konkretnej rodzinie, służy do użycia strzałek w gridzie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewSetMaxSeqArticleCategory(object sender, EventArgs e)
        {
            _view.MaxSeqArticleCategory = _articleCategoriesBL.ReturnMaxSeqArticleCategoryById(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishException = _articleCategoriesBL.InformationException;
        }

               
        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie wczytuje wszystkie pola do wyszukiwarki kategorii
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewLoadFieldsSearch(object sender, EventArgs e)
        {
            ArticleCategoriesDDLBL articleCategoriesDDLBL = new ArticleCategoriesDDLBL();


            _view.ArticleCategoriesDataSource = new DataSourceContainer("Name", "ArticleCategoryID", articleCategoriesDDLBL.ReturnArticleCategoriesDDLSearch(), null);
            _view.StatusDataSource = new DataSourceContainer("Value", "Key", CommonDictionary.Instance.GetStatusDictionary(), null);
            _view.ActiveDataSource = new DataSourceContainer("Value", "Key", CommonDictionary.Instance.GetActiveDictionary(), null);
            _view.PageSizeDataSource = new DataSourceContainer("Value", "Key", CommonDictionary.Instance.GetPageSizeDictionary(), 1);

            _view.PublishException = _articleCategoriesBL.InformationException;            
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie usuwa kategorię
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewDeleteArticleCategory(object sender, EventArgs e)
        {
            ArticleCategoryOperBL _articleCategoryOperBL = new ArticleCategoryOperBL(_view.ArticleCategory.ArticleCategoryID);
            _articleCategoryOperBL.DeleteArticleCategory();
            _view.PublishInformationSuccess = _articleCategoryOperBL.InformationSuccess;
            _view.PublishInformationNotice = _articleCategoryOperBL.InformationNotice;
            _view.PublishException = _articleCategoryOperBL.InformationException;           
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie zmienia kolejność w gridzie o jeden wiersz w górę w podkategorii
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewSetUpSeq(object sender, EventArgs e)
        {
            _articleCategoriesBL.UpSeqArticleCategory(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishInformationSuccess = _articleCategoriesBL.InformationSuccess;
            _view.PublishException = _articleCategoriesBL.InformationException;
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie zmienia kolejność w girdzie o jeden wiersz w dół
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewSetDownSeq(object sender, EventArgs e)
        {
            _articleCategoriesBL.DownSeqArticleCategory(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishInformationSuccess = _articleCategoriesBL.InformationSuccess;
            _view.PublishException = _articleCategoriesBL.InformationException;            
        }
    }
}
